import { defineStore } from 'pinia'
import webApi from '@/apis/web'

export const useConfigStore = defineStore('config', {
  state: () => ({
    data: {},
  }),
  actions: {
    async init() {
      this.fetchConfig()
    },

    async fetchConfig() {
      this.data = (await webApi.config()).data
    },

    get(keyPath = '', defaultValue = undefined) {
      return keyPath.split('.').reduce((acc, key) => {
        return acc && acc[key] !== undefined ? acc[key] : defaultValue
      }, this.data)
    },
  },
})
